package com.example.minelocationdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.example.minelocationdemo.utils.Config;
import com.minedata.location.CoordType;
import com.minedata.location.MineLocation;
import com.minedata.location.MineLocationListener;
import com.minedata.location.MineLocationManager;
import com.minedata.location.MineLocationOptions;
import com.minedata.location.MineLocationTraceUploadListener;
import com.minedata.location.MineLocationTraceUploader;
import com.minedata.location.MineLocationUploadOptions;
import com.minedata.location.MineLocationUploadPos;


public class BasemapActivity extends AppCompatActivity {

    private Context mContext;
    private static String TAG = "BasemapActivityLocation";
    private TextView log_show;
    private NotificationManager notificationManager;
    private boolean isCreateChannel;
    private String locationUrl = "https://service.minedata.cn/service/lbs/location/newposition/";
    private String AuthorizeUrl = "https://service.minedata.cn/service/lbs/location/";

    private String AuthorizeKey = "你申请到的key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.activity_basemap);

        log_show = findViewById(R.id.log_show);
        initLocation();

    }


    private void initLocation() {

        if ("".equals(AuthorizeKey) || "".equals(AuthorizeUrl)) {
            Toast.makeText(BasemapActivity.this, "请输入授权URL和Key", Toast.LENGTH_SHORT).show();

            return;
        }
        MineLocationManager.getInstance().setAgreePrivacy(true);
        MineLocationManager.getInstance().setOutLog(false);
        MineLocationManager.getInstance().init(getApplicationContext(), MineLocationManager.LocationSource.station);
        MineLocationManager.getInstance().addListener(new MineLocationListener() {
            @Override
            public void onSimLocationChanged(MineLocation mineLocation) {

            }

            @Override
            public void onLocationChanged(MineLocation mineLocation) {

                log_show.setText("类型:" + mineLocation.getProvider() +
                        "\n经度:" + mineLocation.getLongitude() + "\n纬度:" + mineLocation.getLatitude()
                        + "\n精度:" + mineLocation.getAccuracy() + "\n角度:" + mineLocation.getBearing()
                        + "\n海拔:" + mineLocation.getAltitude() + "\n速度:" + mineLocation.getSpeed()
                        + "\n时间:" + DateUtils.stampToDate(mineLocation.getTime() + "")
                );

                Log.d(TAG, mineLocation.getProvider() +
                        ";经度:" + mineLocation.getLongitude() + ";纬度:" + mineLocation.getLatitude()
                        + "精度:" + mineLocation.getAccuracy() + "角度:" + mineLocation.getBearing()
                        + "海拔:" + mineLocation.getAltitude() + "速度:" + mineLocation.getSpeed() +
                        "时间:" + DateUtils.stampToDate(mineLocation.getTime() + "")
                );
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                Log.d(TAG, i + "错误码");
            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        });


        MineLocationOptions options = new MineLocationOptions();
        options.setCoordType(CoordType.GCJ02);
        options.setGpsInterval(1000);
        options.setNetWorkInterval(1000);
        options.setStationInterval(5000);
        options.setStationUrl(locationUrl);
        options.setAuthorizeUrl(AuthorizeUrl);
        options.setAuthorizeKey(AuthorizeKey);
        MineLocationManager.getInstance().enableForegroundLocation(110, buildNotification());
        MineLocationManager.getInstance().start(options);

    }


    private Notification buildNotification() {
        Notification.Builder builder = null;
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= 26) {
            //Android O上对Notification进行了修改，如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
            if (notificationManager == null) {
                notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }
            String channelId = getPackageName();
            if (!isCreateChannel) {
                NotificationChannel notificationChannel = new NotificationChannel(channelId,
                        "123", NotificationManager.IMPORTANCE_DEFAULT);
                notificationChannel.enableLights(true);//是否在桌面icon右上角展示小圆点
                notificationChannel.setLightColor(Color.BLUE); //小圆点颜色
                notificationChannel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
                notificationManager.createNotificationChannel(notificationChannel);
                isCreateChannel = true;
            }
            builder = new Notification.Builder(getApplicationContext(), channelId);
        } else {
            builder = new Notification.Builder(getApplicationContext());
        }
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("MineLocationDemo")
                .setContentText("正在后台运行")
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setWhen(System.currentTimeMillis());

        if (android.os.Build.VERSION.SDK_INT >= 16) {
            notification = builder.build();
        } else {
            notification = builder.getNotification();
        }
        return notification;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();


        MineLocationTraceUploader.getInstance().stop();
    }


}